package pl.un4giv3n.mightvsmagic.action;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.entity.Action;
import pl.un4giv3n.mightvsmagic.entity.ActionType;
import pl.un4giv3n.mightvsmagic.entity.ActivityType;
import pl.un4giv3n.mightvsmagic.entity.Item;
import pl.un4giv3n.mightvsmagic.entity.ItemTemplate;
import pl.un4giv3n.mightvsmagic.entity.UserActivity;
import pl.un4giv3n.mightvsmagic.utils.ActionUtil;
import pl.un4giv3n.mightvsmagic.utils.ActivityLogUtil;

/**
 * @author andrzejewskia
 * action performing buing of an item on the bazar
 */
public class BuyAction extends BazarPageAction {

	private static final long serialVersionUID = 1L;
	/**
	 *  bought item id
	 */
	private long itemId;
	/* (non-Javadoc)
	 * @see pl.un4giv3n.mightvsmagic.action.PersistenceAction#execute()
	 */
	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if (user == null) {
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		Action currentAction = user.getCurrentAction();
		if (currentAction == null || currentAction.equals(Action.NO_ACTION) || currentAction.getActionType().equals(ActionType.FIGHT)) {
			ItemTemplate itemTemplate = entityManager.find(
					ItemTemplate.class, itemId);
			entityManager.getTransaction().begin();
			if (!canAfford(itemTemplate)) {
				addActionError("Nie stać cię na ten przedmiot");
				prepareEq(entityManager);
				entityManager.getTransaction().rollback();
				return INPUT;
			} else {
				Item item = new Item(itemTemplate.getName(), itemTemplate
						.getType(), itemTemplate.getImgUrl(), itemTemplate
						.getMinDef(), itemTemplate.getMaxDef(), itemTemplate
						.getMinDmg(), itemTemplate.getMaxDmg(), itemTemplate
						.getPrice(), itemTemplate.getPremiumPrice(), itemTemplate.getRequiredFightExp(),
						itemTemplate.getRequiredMagicExp(), itemTemplate.getRequiredAnyExp(), itemTemplate.getStrenghtModifier(),
						itemTemplate.getStaminaModifier(), itemTemplate.getManaModifier(),
						itemTemplate.getAttackModifier(),itemTemplate.getParryModifier(), itemTemplate.getSpeedModifier(),
						itemTemplate.getStrenghtReq(), itemTemplate.getStaminaReq(),
						itemTemplate.getManaReq(),itemTemplate.getAttackReq(),
						itemTemplate.getParryReq(), itemTemplate.getId());
				entityManager.persist(item);
				user.getItems().add(item);
				user.removeGold(item.getPrice());
				user.setDragonTeeth(user.getDragonTeeth() - itemTemplate.getPremiumPrice());
				UserActivity activity = ActivityLogUtil.createActivity(ActivityType.ITEM_BOUGHT, user.getId(), "Kupiony przedmiot: " + item.toString());
				entityManager.persist(activity);
				entityManager.merge(user);
				entityManager.flush();
				entityManager.getTransaction().commit();
				entityManager.close();
				//set this to redirect to right bazar tab
				selectedType = item.getType();
				addActionMessage("Dokonałeś zakupu przedmiotu!");
				return SUCCESS;
			}
		} else {
			entityManager.close();
			addActionError(ActionUtil.generateErrorMessage(currentAction, user));
			return ERROR;
		}
	}

	private boolean canAfford(ItemTemplate itemTemplate) {
		return itemTemplate.getPrice()<= user.getGold() && itemTemplate.getPremiumPrice() <= user.getDragonTeeth();
	}

	public long getItemId() {
		return itemId;
	}

	public void setItemId(long itemId) {
		this.itemId = itemId;
	}

}
